package com.umeox.watch.moto.launcher.adapter;

import android.content.ContentValues;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.utils.DateUtils;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.launcher.R;

import java.util.List;

public class HeartRateAdapter extends RecyclerView.Adapter<HeartRateAdapter.Holder> {

    public List<ContentValues> getDataList() {
        return mDataList;
    }

    private List<ContentValues> mDataList;

    private Context mContext;


    public HeartRateAdapter(Context context, List<ContentValues> datas) {
        mContext = context;
        mDataList = datas;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_heart_layout, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Holder viewHolder = (Holder) holder;
        viewHolder.bind(mDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setDataList(List<ContentValues> dataList) {
        mDataList.clear();
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }


    protected class Holder extends RecyclerView.ViewHolder {
        private TextView mDateTv;
        private TextView mHeartTv;


        public Holder(@NonNull View itemView) {
            super(itemView);
            mDateTv = itemView.findViewById(R.id.tv_date);
            mHeartTv = itemView.findViewById(R.id.tv_heart);
        }


        public void bind(ContentValues values) {
            mDateTv.setText(DateUtils.formatDateTime(values.getAsLong(DataProvider.HeartRateProvider.MEASURE_TIME), "hh:mma"));
            mHeartTv.setText(String.format("%sBPM", values.getAsString(DataProvider.HeartRateProvider.AVERAGE_VALUE)));
        }
    }

}
